# 导包
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium import webdriver
import pickle
import os
import time


# 需要的URL
damai_url = 'https://www.damai.cn/'
# 登录页
login_url = 'https://passport.damai.cn/login?ru=https%3A%2F%2Fwww.damai.cn%2F'
# 目标页
target_url = 'https://detail.damai.cn/item.htm?spm=a2oeg.home.card_0.ditem_1.591b23e1cDKzkq&id=703896301262'


class Dama:
    def __init__(self):
        self.status = 0
        self.login_method = 1
        path = Service('C://Users//25238//PycharmProject//planedemo//Pycharm//msedgedriver.exe')
        self.dirve = webdriver.Edge(service=path)
        
    def set_cookie(self):
        self.dirve.get(damai_url)
        while self.dirve.title.find('大麦网-全球演出赛事官方购票平台') != -1:
            time.sleep(1)
            print('----- 请点击登录 -----')
            pass
        while self.dirve.title != '大麦网-全球演出赛事官方购票平台-100%正品、先付先抢、在线选座！':
            time.sleep(1)
            print('----- 请扫码 -----')
            pass
        print('----- 登录成功 -----')
        pickle.dump(self.dirve.get_cookies(), open('cookies.pkl', 'wb'))
        print('----- cookie设置成功 -----')
        self.dirve.get(target_url)

    def get_cookie(self):
        try:
            cookies = pickle.load(open('cookies.pkl','rb'))
            print("cookies打开成功")
            for cookie in cookies:
                cookie_dict = {
                    'domain': '.damai.cn',
                    'name': cookie.get('name'),
                    'value': cookie.get('value')
                }
                self.dirve.add_cookie(cookie_dict)
                pass
        except Exception as e:
            print(e)

    def login(self):
        if self.login_method == 0:
            self.dirve.get(login_url)
            # 载入登录界面
            print("开始登录")
        elif self.login_method == 1:
            if not os.path.exists('cookies.pkl'):
                self.set_cookie()
            else:
                self.dirve.get(target_url)
                self.get_cookie()
                self.dirve.get(target_url)
                time.sleep(5)
        pass

    def enter_concert(self):
        print('----- 打开浏览器 -----')
        self.login()
        self.dirve.refresh()
        self.status = 2
        print('----- 登录成功 -----')

        time.sleep(30)

    def click_find_elements(self, element):
        bro = self.dirve
        try:
            botton = bro.find_element(By.CLASS_NAME, element)
            botton.click()
            print('----- 点击成功 -----')
        except Exception as e:
            print("----- 找不到图标 ------")
            print(e)
        pass

    def chooose_ticket(self):
        if self.status == 2:
            self.click_find_elements('buy-link')
            print('='*20)
            print('----- 请选择日期和票价 -----')
            while self.dirve.title.find('提交订单') == -1:
                pass


if __name__ == '__main__':
    try:
        con = Dama()
        con.enter_concert()
    except Exception as e:
        print(e)




